﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Sprinkles.Tests.DeploymentHosts
{
    public class MethodOverrideHost : DeploymentHost
    {
        protected override bool Include
        {
            get { return this.UserContextIsNotMarked(); }
        }

        //put this here, then override in ToOverride, in order to check protected access
        protected virtual Filler GetFiller()
        {
            return null;
        }

        public static string FILLER_NAME = Qualify("filler");
        public static string FILLER2_NAME = Qualify("filler2");
        public static string TO_OVERRIDE_NAME = Qualify("to-override");
        public static string TO_NOT_OVERRIDE_NAME = Qualify("not-to-override");

        public static string ORIGINAL_TEXT = "nobody";
        public static string CHANGED_TEXT = "a filler";
        public static string FILLING_IN_TEXT = "just filling in for an object";

        public class Filler
        {
            public string WhoAmI = CHANGED_TEXT;
        }
        public class Filler2 : Filler
        {
            public Filler2()
            {
                this.WhoAmI = FILLING_IN_TEXT;
            }
        }
        public class ToOverride : MethodOverrideHost
        {
            protected override bool Include { get { return false; } }
            public string ShowWhoIAm()
            {
                return GetFiller().WhoAmI;
            }
            protected override Filler GetFiller()
            {
                return new Filler() { WhoAmI = ORIGINAL_TEXT };
            }
            public virtual object GetObject()
            {
                return null;
            }

            protected virtual object GetObject2()
            {
                return null;
            }
            public object FetchObject2()
            {
                return GetObject2();
            }
        }

        public static string Qualify(string name)
        {
            return "MethodOverrideHost." + name;
        }

        protected override void RegisterDeployments()
        {
            base.Register<Filler>()
                .Name(FILLER_NAME)
                .Singleton(false)
                .Done();

            base.Register<Filler2>()
                .Name(FILLER2_NAME)
                .Singleton(false)
                .Done();

            base.Register<ToOverride>()
                .Name(TO_OVERRIDE_NAME)
                .Singleton(true)
                .MethodOverride(x => x.GetFiller(), FILLER_NAME)
                .MethodOverride(x => x.GetObject(), FILLER2_NAME)
                .MethodOverride("GetObject2", FILLER2_NAME)
                .Done();

            base.Register<ToOverride>()
                .Name(TO_NOT_OVERRIDE_NAME)
                .Singleton(true)
                .Done();
        }
    }
}
